package com.patMall.controller;

import com.patMall.entity.BrowsingHistory;
import com.patMall.service.BrowsingHistoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.controller
 * @Date 2024/2/13 9:36
 * @Version 1.0
 */
@Controller
@RequestMapping("browsingHistory")
public class BrowsingHistoryController {
    @Resource
    private BrowsingHistoryService browsingHistoryService;

    @PostMapping("addBrowsingHistory")
    @ResponseBody
    public String addBrowsingHistory(BrowsingHistory browsingHistory) {
        BrowsingHistory browsingHistory1 = browsingHistoryService.getBrowsingHistory(browsingHistory.getCommodityId());
        System.err.println(browsingHistory1);
        if (browsingHistory1 != null) {
            long timestamp = System.currentTimeMillis();
            Date date = new Date(timestamp);
            browsingHistory1.setBrowsingHistoryTime(date);
            System.err.println("b1" + browsingHistory1);
            int count = browsingHistoryService.updateBrowsingHistory(browsingHistory1);
        } else {
            long timestamp = System.currentTimeMillis();
            Date date = new Date(timestamp);
            browsingHistory.setBrowsingHistoryTime(date);
            System.err.println("b" + browsingHistory);
            int count = browsingHistoryService.addBrowsingHistory(browsingHistory);
            if (count == 1) {
                return "success";
            }
        }
        return "fail";
    }
    @PostMapping("getBrowsingHistoryUser")
    @ResponseBody
    public Object getBrowsingHistoryUser(Integer userId){
        List<BrowsingHistory> browsingHistoryList = browsingHistoryService.getBrowsingHistoryCommodity(userId);
        System.err.println(browsingHistoryList);
        Map map = new HashMap();
        if (browsingHistoryList.size() > 0) {
            map.put("data", browsingHistoryList);
            map.put("msg", "success");
        }else {
            map.put("msg","NoFound");
        }
        return map;
    }
}
